﻿using CMS.BaseEntity;
using ElectronicSelling.DataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace CMS.DataAccess
{
    public class ProductAccess
    {
        public DataTable GetListBonus()
        {
            return DataAccess.ExeStoredNonToDataTable("cusp_BonusSelectAll");
        }

        public DataTable FindAllByNameQuantity(Product product)
        {
            return DataAccess.ExeStoredWithParamToDataTable(
                "cusp_ProductSelectAllByNameQuantity",
                "@Name", product.Name,
                "@Quantity", product.Quantity
                );
        }

        public DataTable FindAllByName(Product product)
        {
            return DataAccess.ExeStoredWithParamToDataTable(
                "cusp_ProductSelectAllByName",
                "@Name", product.Name
                );
        }

        public DataTable FindAllByQuantity(Product product)
        {
            return DataAccess.ExeStoredWithParamToDataTable(
                "cusp_ProductSelectAllByQuantity",
                "@Quantity", product.Quantity
                );
        }

        public DataTable FindByEnableNameQuantity(Product product)
        {
            return DataAccess.ExeStoredWithParamToDataTable(
                "cusp_ProductSelectByEnableNameQuantity",
                "@Enable", product.Enable,
                "@Name", product.Name,
                "@Quantity", product.Quantity
                );
        }

        public DataTable FindByEnableName(Product product)
        {
            return DataAccess.ExeStoredWithParamToDataTable(
                "cusp_ProductSelectByEnableName",
                "@Enable", product.Enable,
                "@Name", product.Name
                );
        }

        public DataTable FindByEnableQuantity(Product product)
        {
            return DataAccess.ExeStoredWithParamToDataTable(
                "cusp_ProductSelectByEnableQuantity",
                "@Enable", product.Enable,
                "@Quantity", product.Quantity
                );
        }

        public DataTable FindByEnable(Product product)
        {
            return DataAccess.ExeStoredWithParamToDataTable(
                "cusp_ProductSelectByEnable",
                "@Enable", product.Enable
                );
        }

        public DataTable GetBySection(Product product)
        {
            return DataAccess.ExeStoredWithParamToDataTable(
                "cusp_ProductSelectBySection",
                "@SectionId", product.SectionId
                );
        }

        public DataTable GetByCategory(Product product)
        {
            return DataAccess.ExeStoredWithParamToDataTable(
                "cusp_ProductSelectByCategory",
                "@CategoryId", product.CategoryId
                );
        }

        public DataTable GetListProduct()
        {
            return DataAccess.ExeStoredNonToDataTable("cusp_ProductSelectAll");
        }
        //public bool IsInsertProduct(Product product)
        //{
        //    return DataAccess.ExeStoredWithParamToBool(
        //        "cusp_ProductInsert", "@CategoryId", product.CategoryId,
        //        "@SectionId", product.SectionId,
        //        "@AuthorId", product.AuthorId,
        //        "@Name", product.Name,
        //        "@Description", product.Description,
        //        "@Intro", product.Intro,
        //        "@Manufacture", product.Manufacture,
        //        "@Guarantee", product.Guarantee,
        //        "@CurrentPrice", product.CurrentPrice,
        //        "@BonusId", product.BonusId,
        //        "@Enable", product.Enable,
        //        "@Quantity", product.Quantity,
        //        "@CreateDate", product.CreateDate,
        //        "@Focus", product.Focus,
        //        "@Image", product.Image
        //        );
        //}

        public bool IsInsertProduct(Product product)
        {
            SqlConnection conn = Connection.GetConnection();
            try
            {
                SqlCommand cmd = new SqlCommand("cusp_ProductInsert", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@CategoryId", SqlDbType.Int).Value = product.CategoryId;
                cmd.Parameters.Add("@SectionId", SqlDbType.Int).Value = product.SectionId;
                cmd.Parameters.Add("@AuthorId", SqlDbType.Int).Value = product.AuthorId;
                cmd.Parameters.Add("@Name", SqlDbType.NText).Value = product.Name;
                cmd.Parameters.Add("@Description", SqlDbType.NText).Value = product.Description;
                cmd.Parameters.Add("@Intro", SqlDbType.NText).Value = product.Intro;
                cmd.Parameters.Add("@Manufacture", SqlDbType.NVarChar).Value = product.Manufacture;
                cmd.Parameters.Add("@Guarantee", SqlDbType.Int).Value = product.Guarantee;
                cmd.Parameters.Add("@CurrentPrice", SqlDbType.Decimal).Value = product.CurrentPrice;
                cmd.Parameters.Add("@BonusId", SqlDbType.Int).Value = product.BonusId;
                cmd.Parameters.Add("@Enable", SqlDbType.Bit).Value = product.Enable;
                cmd.Parameters.Add("@Quantity", SqlDbType.Int).Value = product.Quantity;
                cmd.Parameters.Add("@CreateDate", SqlDbType.DateTime).Value = product.CreateDate;
                cmd.Parameters.Add("@Focus", SqlDbType.Bit).Value = product.Focus;
                cmd.Parameters.Add("@Image", SqlDbType.NVarChar).Value = product.Image;
                conn.Open();
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                conn.Dispose();
            }
        }

        public bool IsUpdated(Product product)
        {
            return DataAccess.ExeStoredWithParamToBool(
                "cusp_ProductUpDate",
                "@ProductId", product.ProductId,
                "@CategoryId", product.CategoryId,
                "@SectionId", product.SectionId,
                "@AuthorId", product.AuthorId,
                "@Name", product.Name,
                "@Description", product.Description,
                "@Intro", product.Intro,
                "@Manufacture", product.Manufacture,
                "@Guarantee", product.Guarantee,
                "@Image", product.Image,
                "@CurrentPrice", product.CurrentPrice,
                "@OldPrice", product.OldPrice,
                "@BonusId", product.BonusId,
                "@Enable", product.Enable,
                "@Quantity", product.Quantity,
                "@LastModify", product.LastModify,
                "@Focus", product.Focus
                );
        }

        public DataTable GetById(Product product)
        {
            return DataAccess.ExeStoredWithParamToDataTable(
                "cusp_ProductSelectByID",
                "@ProductId", product.ProductId
                );
        }

        public bool IsDeleted(Product product)
        {
            return DataAccess.ExeStoredWithParamToBool(
                "cusp_ProductDelete",
                "@ProductId", product.ProductId
                );
        }
    }
}